setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top3domText <- read.csv("textpool/top3domText.csv", stringsAsFactors = FALSE)

### prepare tasks
LInarrowcursory <- validateLabel(type = "LI", n = 500, 
                                 text.predict = top3domText, text.name = "post_text",
                                 labels = c("Working Class",
                                            "Planned Parenthood",
                                            "Farm Bill",
                                            "Economy",
                                            "Prescription Medicine",
                                            "Grants for Colleges",
                                            "Banking",
                                            "Government Spending",
                                            "Healthcare",
                                            "Debt Ceiling"), 
                                 labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                                 labels.add = NULL)

goldLI <- read.csv("goldstandard/goldLI.csv", stringsAsFactors = FALSE)
LInarrowcursory <- mixGold(tasks = LInarrowcursory, golds = goldLI)
LInarrowcursory <- recordTasks(type = "LI", tasks = LInarrowcursory,
                               path = "labelrecords/LInarrowcursory.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# LInarrowcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                 tasksrecord = LInarrowcursory, HITidspath = "HITids/LInarrowcursoryids1.Rdata")
# ### extend HITs if needed
# for(i in LInarrowcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LInarrowcursoryids1.Rdata")
# LInarrowcursoryresults1 <- getResults(batch_id = "LInarrowcursory1", hit_ids = HITids, retry = FALSE)
# write.csv(LInarrowcursoryresults1, "LI/results/LInarrowcursoryresults1.csv", row.names = FALSE)
# evalResults(results = LInarrowcursoryresults1, key = LInarrowcursory, type = "LI")
# 
# ### send tasks (second trial)
# LInarrowcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                 tasksrecord = LInarrowcursory, HITidspath = "HITids/LInarrowcursoryids2.Rdata")
# ### extend HITs if needed
# for(i in LInarrowcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LInarrowcursoryids2.Rdata")
# LInarrowcursoryresults2 <- getResults(batch_id = "LInarrowcursory2", hit_ids = HITids, retry = FALSE)
# write.csv(LInarrowcursoryresults2, "LI/results/LInarrowcursoryresults2.csv", row.names = FALSE)
# evalResults(results = LInarrowcursoryresults2, key = LInarrowcursory, type = "LI")